﻿// test1.cpp: 定义应用程序的入口点。
//

#include "test1.h"

using namespace std;

/*
* 创建窗体的过程
* 1、初始化GLFW的基本环境
* 2、创建窗体对象
* 3、执行窗体循环
*/

void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);

//设置窗口的宽高
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;

void keyCallBack(GLFWwindow* window, int key, int scancode, int action, int mods)
{

}

int main()
{
    //初始化glfw
    glfwInit();
    //opengl版本号
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    //使用核心模式（非立即渲染模式）
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    //创建窗口
    GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
    if (window == NULL)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);  ///设置当前窗体为opengl绘制的舞台
    //窗体size回调
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

    //设置按键监听，监听回调
    glfwSetKeyCallback(window, keyCallBack);


    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }

    //窗体循环
    //渲染
    while (!glfwWindowShouldClose(window))
    {
        //输入
        processInput(window);

        // render
       // ------
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        //事件
        //函数会交换颜色缓冲（它是一个储存着GLFW窗口每一个像素颜色值的大缓冲），它在这一迭代中被用来绘制，并且将会作为输出显示在屏幕上。
        glfwSwapBuffers(window);
        glfwPollEvents();   //接收并分发窗体时间
    }

    //释放
    glfwTerminate();  //退出程序
    return 0;
}


void processInput(GLFWwindow* window)
{
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true);
}


void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
    glViewport(0, 0, width, height);
}